package jbrowser.gui;

import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Window extends JFrame implements ChangeListener {
    
    @Override
    public void stateChanged(ChangeEvent e) {
        if (e.getSource().equals(tabbedPane)) {
            if (tabbedPane.getTabCount() == 1) {
                this.processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
            } else if (tabbedPane.getSelectedComponent().equals(newTabAdder)) {
                tabbedPane.removeChangeListener(this);
                newTab();
                tabbedPane.addChangeListener(this);
            } else {
                Tab tab = (Tab) tabbedPane.getSelectedComponent();
                this.setTitle(tab.getTitle() + " - JBrowser");
            }
        }
    }
    
    public Window() {
        initComponents();
        newTab();
        tabbedPane.addChangeListener(this);
        tabbedPane.setSelectedIndex(0);
    }
    
    public final Tab newTab() {
        Tab tab = new Tab(this);
        tabbedPane.add(tab, tabbedPane.getTabCount() - 1);
        tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 2, tab.getTabComponent());
        tabbedPane.setSelectedComponent(tab);
        return tab;
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabbedPane = new javax.swing.JTabbedPane();
        newTabAdder = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("JBrowser");

        tabbedPane.addTab("   ", null, newTabAdder, "New Tab");

        getContentPane().add(tabbedPane, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel newTabAdder;
    private javax.swing.JTabbedPane tabbedPane;
    // End of variables declaration//GEN-END:variables
}
